package com.yuji.aacar.core.cores.utils;

import android.content.SharedPreferences;
import android.util.Base64;

import com.google.gson.Gson;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import static com.yuji.aacar.core.cores.utils.CoreENVConfig.sp;

/**
 * <li>Package:com.ttp.core.mvvm.appbase
 * <li>Author: lining
 * <li>Date: 2017/2/14
 * <li>Description:
 * <br> 持久化对象 </br>
 */
public class CorePersistenceUtil {
	public final static String TAG = "CorePersistenceUtil";
	public final static String USERID = "userId"; //用户id
	public final static String PHONENUMBER = "phoneNumber";//用户手机号


	/**
	 * 储存一个对象到share preferences
	 */
	public static <T> void saveObjectToSharePreferes(T object, String key) {
		if (sp == null) {
			LogUtil.e(TAG,"please config environment first:ENVConfig.configurationEnvironment(context)");
		}else {
			if (object != null && key != null) {
				SharedPreferences.Editor editor = sp.edit();
//				String objStr = CoreJsonHandler.objectToJson(object);
//				LogUtil.d(TAG,"add to sp json:"+objStr);
//				editor.putString(key, objStr);
				editor.commit();
			}
		}
	}

	
	/**
	 * 储存一个对象到share preferences
	 */
	public static <T> void saveListToSharePreferences(List<?> object, String key) {
		if (sp == null) {
			LogUtil.e(TAG,"please config environment first:ENVConfig.configurationEnvironment(context)");
		}else {
			if (object != null && key != null) {
				SharedPreferences.Editor editor = sp.edit();
				Gson gson = new Gson();  
				String objStr = gson.toJson(object);
				LogUtil.d(TAG,"add to sp json:"+objStr);
				editor.putString(key, objStr);
				editor.commit();
			}
		}
	}

	
	/**
	 *  从share preferences获取一个对象   cls:User.class
	 * 
	 * @param key  
	 * @param cls  该对象的类  例如：User.class
	 * @return
	 */
//	public static <T> T getObjectFromSharePreferences(String key, Class<T> cls) {
//		if (sp == null) {
//			LogUtil.e(TAG,"please config environment first:ENVConfig.configurationEnvironment(context)");
//		}else {
//			String jsonStr = sp.getString(key, "");
//			LogUtil.d(TAG,"json from sp:"+jsonStr);
//			if (jsonStr.equals("") || jsonStr == null) {
//				return null;
//			}else {
//				return CoreJsonHandler.jsonToObject(jsonStr, cls);
//			}
//		}
//		return null;
//	}

	
	/**
	 *  从share preferences获取一个对象   cls:User.class
	 * @param <T>
	 * 
	 * @param key  
	 * @param cls  该对象的类  例如：User.class
	 * @return
	 */
//	public static <T> List<?> getListFromSharePreferences(String key, Class<T> cls) {
//		if (sp == null) {
//			LogUtil.e(TAG,"please config environment first:ENVConfig.configurationEnvironment(context)");
//		}else {
//			String jsonStr = sp.getString(key, "");
//			LogUtil.d(TAG,"json from sp:"+jsonStr);
//			if (jsonStr.equals("") || jsonStr == null) {
//				return null;
//			}else {
//				return CoreJsonHandler.jsonToList(jsonStr, cls);
//			}
//		}
//		return null;
//	}
	
//	public static <T> List<?> getListFromSharePreferences(String key, Type type) {
//		if (sp == null) {
//			LogUtil.e(TAG,"please config environment first:ENVConfig.configurationEnvironment(context)");
//		}else {
//			String jsonStr = sp.getString(key, "");
//			LogUtil.d(TAG,"json from sp:"+jsonStr);
//			if (jsonStr.equals("") || jsonStr == null) {
//				return null;
//			}else {
//				return CoreJsonHandler.jsonToList(jsonStr, type);
//			}
//		}
//		return null;
//	}


	/*********
	 * 保存对象
	 * @param key 存入的key 名
	 * @param object
	 */
	public static void saveObject(String key, Object object){
		// 创建字节输出流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			// 创建对象输出流，并封装字节流
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			// 将对象写入字节流
			oos.writeObject(object);
			// 将字节流编码成base64的字符窜
			String value = new String(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
			SharedPreferences.Editor editor = sp.edit();
			editor.putString(key, value);
			editor.commit();

			oos.flush();
			oos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/********
	 * 获取保存的对象
	 * @param key
	 * @return
	 */
	public static Object getObject(String key){
		Object object = null;
		String value = sp.getString(key, null);
		if(value == null){
			return null;
		}
		//读取字节
		byte[] base64 = Base64.decode(value.getBytes(), Base64.DEFAULT);

		//封装到字节流
		ByteArrayInputStream bais = new ByteArrayInputStream(base64);

		try {
			//再次封装
			ObjectInputStream bis = new ObjectInputStream(bais);
			//读取对象
			object = bis.readObject();
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}

		return object;
	}


	/**
	 * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
	 * @param key
	 * @param object
	 */
	public static void setParam(String key, Object object){

		String type = object.getClass().getSimpleName();
		SharedPreferences.Editor editor = sp.edit();

		if("String".equals(type)){
			editor.putString(key, (String)object);
		}
		else if("Integer".equals(type)){
			editor.putInt(key, (Integer)object);
		}
		else if("Boolean".equals(type)){
			editor.putBoolean(key, (Boolean)object);
		}
		else if("Float".equals(type)){
			editor.putFloat(key, (Float)object);
		}
		else if("Long".equals(type)){
			editor.putLong(key, (Long)object);
		}

		editor.commit();
	}

	/**
	 * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
	 * @param key
	 * @param defaultObject
	 * @return
	 */
	public static Object getParam(String key, Object defaultObject){
		String type = defaultObject.getClass().getSimpleName();
		if("String".equals(type)){
			return sp.getString(key, (String)defaultObject);
		}
		else if("Integer".equals(type)){
			return sp.getInt(key, (Integer)defaultObject);
		}
		else if("Boolean".equals(type)){
			return sp.getBoolean(key, (Boolean)defaultObject);
		}
		else if("Float".equals(type)){
			return sp.getFloat(key, (Float)defaultObject);
		}
		else if("Long".equals(type)){
			return sp.getLong(key, (Long)defaultObject);
		}

		return null;
	}

}
